package com.zmn.oms.business.impl.completecode;

import com.zmn.manager.redis.RedisManager;
import com.zmn.oms.business.interfaces.completecode.CompleteCodeBService;
import com.zmn.oms.common.annotation.ZsNormalOrderWorkLog;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.constant.RedisKeyConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.common.exception.OmsBusinessException;
import com.zmn.oms.model.dto.completecode.CompleteCodeDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 类描述：
 *
 * @author liuying
 * @date 2021/05/06 17:03
 */
@Slf4j
@Service
public class CompleteCodeBServiceImpl implements CompleteCodeBService {

    @Resource
    private RedisManager redisManager;

    @Override
    public void verifyCompleteCodeExists(Long orderId, Long workId) throws OmsBaseException {
        String redisKey = String.format(RedisKeyConsts.ORDER_COMPLETE_CODE_KEY, workId);
        if (!redisManager.exists(redisKey)) {
            throw new OmsBaseException("操作失败，请前往工程师端核销码页填写核销码");
        }
    }

    @Override
    public boolean completeCodeExists(Long workId) {
        String redisKey = String.format(RedisKeyConsts.ORDER_COMPLETE_CODE_KEY, workId);
        return redisManager.exists(redisKey);
    }

    @Override
    public String getCompleteCode(Long workId) {
        String redisKey = String.format(RedisKeyConsts.ORDER_COMPLETE_CODE_KEY, workId);
        return redisManager.get(redisKey);
    }

    @Override
    @ZsNormalOrderWorkLog(type = OrderLogConsts.ORDER_LOG_TYPE_INPUT_COMPLETE_CODE)
    public void addCompleteCode(CompleteCodeDTO completeCodeDTO) {
        // 保存到redis
        String redisKey = String.format(RedisKeyConsts.ORDER_COMPLETE_CODE_KEY, completeCodeDTO.getWorkId());
        redisManager.setex(redisKey, completeCodeDTO.getCompleteCode(), 3600 * 24 * 30);
    }
}
